package com.hellobike.haxing.modules.supplier.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.supplier.dto.TSupplierContractDto;
import com.hellobike.haxing.modules.supplier.entity.TSupplierContract;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInvoice;
import com.hellobike.haxing.modules.supplier.service.ITSupplierContractService;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInvoiceService;
import com.hellobike.haxing.modules.supplier.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Description: 供应商合同表
 * @Author: haxing-boot
 * @Date:   2020-01-16
 * @Version: V1.0
 */
@RestController
@RequestMapping("/supplier/tSupplierContract")
@Slf4j
public class TSupplierContractController extends BaseController<TSupplierContract, ITSupplierContractService> {
	@Autowired
	private ITSupplierContractService tSupplierContractService;

	@Autowired
	private ITSupplierInfoService tSupplierInfoService;

	@Autowired
	private ITSupplierInvoiceService tSupplierInvoiceService;


	/**
	 * 分页列表查询
	 *
	 * @param tSupplierContract
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "供应商合同表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TSupplierContract tSupplierContract,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TSupplierContract> queryWrapper = QueryGenerator.initQueryWrapper(tSupplierContract, req.getParameterMap());
		Page<TSupplierContract> page = new Page<TSupplierContract>(pageNo, pageSize);
		IPage<TSupplierContract> pageList = tSupplierContractService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 通过id查询
	 *
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/detail")
	public Result<?> detail(@RequestBody JSONObject json,HttpServletRequest req) {
		Long id = json.getLong("headerId");
		TSupplierContractDto tSupplierContractDto = new TSupplierContractDto();
		TSupplierContract tSupplierContract = tSupplierContractService.getById(id);
		if(tSupplierContract==null) {
			return Result.error("未找到对应数据");
		}else{
			BeanUtils.copyProperties(tSupplierContract,tSupplierContractDto);
			if(tSupplierContract.getSupplierInfoId() != null) {
				List<TSupplierInfo> tSupplierInfos = new ArrayList<>();
				TSupplierInfo tSupplierInfo = tSupplierInfoService.getById(tSupplierContract.getSupplierInfoId());
				tSupplierInfos.add(tSupplierInfo);
				tSupplierContractDto.setTSupplierInfos(tSupplierInfos);
			}
			if(tSupplierContract.getInvoiceId() != null) {
				TSupplierInvoice tSupplierInvoice = new TSupplierInvoice();
				tSupplierInvoice.setSupplierInfoId(tSupplierContract.getSupplierInfoId());
				QueryWrapper<TSupplierInvoice> queryWrapper = QueryGenerator.initQueryWrapper(tSupplierInvoice, req.getParameterMap());
				List<TSupplierInvoice> tSupplierInfos = tSupplierInvoiceService.getBySupplierId(queryWrapper);
				tSupplierContractDto.setTSupplierInvoices(tSupplierInfos);
			}
		}

		return Result.ok(tSupplierContractDto);
	}

	/**
	 *   添加
	 *
	 * @param tSupplierContract
	 * @return
	 */
	@AutoLog(value = "供应商合同表-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TSupplierContract tSupplierContract) {
		tSupplierContractService.save(tSupplierContract);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param tSupplierContract
	 * @return
	 */
	@AutoLog(value = "供应商合同表-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TSupplierContract tSupplierContract) {
		// 修改合同过期时间
		if(tSupplierContract.getEndTime() != null && tSupplierContract.getEndTime().after(new Date())){
			tSupplierContract.setState(2);
		}
		tSupplierContractService.updateById(tSupplierContract);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "供应商合同表-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		tSupplierContractService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "供应商合同表-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tSupplierContractService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		TSupplierContract tSupplierContract = tSupplierContractService.getById(id);
		if(tSupplierContract==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(tSupplierContract);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param tSupplierContract
	 */
	@AutoLog(value = "供应商合同表-导出excel",operateType=6)
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, TSupplierContract tSupplierContract) {
		return super.exportXls(request, tSupplierContract, TSupplierContract.class, "供应商合同表");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@AutoLog(value = "供应商合同表-导入excel",operateType=5)
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, TSupplierContract.class);
	}

}
